Freemarker页面语法
A 概念
最常用的 3 个概念
sequence 序列,对应java 里的list 、数组等非键值对的集合
hash 键值对的集合
namespace 对一个ftl 文件的引用, 利用这个名字可以访问到该ftl 文件的资源
B 指令
if, else, elseif
语法
Java代码
...
...
...
...
...
...
...
...
...
...
用例
Freemarker代码
x is 1
x is 1
x is not 1
x is 1
x is 1
x is not 1
switch, case, default, break
语法
Freemarker代码
...
...
...
...
...
...
...
...
...
...
用例
字符串
Freemarker代码
This will be processed if it is small
This will be processed if it is medium
This will be processed if it is large
This will be processed if it is neither
This will be processed if it is small
This will be processed if it is medium
This will be processed if it is large
This will be processed if it is neither
数字
Freemarker代码
1
2
d
1
2
d
如果x=1 输出 1 2, x=2 输出 2, x=3 输出d
list, break
语法
Freemarker代码
...
...
...
...
关键字
item_index:是list当前值的下标
item_has_next:判断list是否还有值
用例
Freemarker代码
${x_index + 1}. ${x},
${x_index + 1}. ${x},
输出:
1.winter,
2.spring,
3.summer,
4.autumn
include
语法
Freemarker代码
或则
Java代码
options包含两个属性
encoding="GBK" 编码格式
parse=true 是否作为ftl语法解析,默认是true,false就是以文本方式引入.注意在ftl文件里布尔值都是直接赋值
的如parse=true,而不是parse="true"
用例
/common/copyright.ftl 包含内容
Ftl代码
Copyright 2001-2002 ${me}
All rights reserved.
Copyright 2001-2002 ${me}
All rights reserved.
模板文件
Java代码
Some test
Yeah
___________________________________________________________________________
Some test
Yeah
___________________________________________________________________________
输出结果:
Some test
Yeah.
Copyright 2001-2002 Juila Smith
All rights reserved.
Import
语法
Freemarker代码
类似于java里的import,它导入文件,然后就可以在当前文件里使用被导入文件里的宏组件
用例
假设mylib.ftl 里定义了宏copyright 那么我们在其他模板页面里可以这样使用
Freemarker代码
|